package com.mresources.web.controller;

import com.mresources.common.dto.Result;
import com.mresources.common.dto.bo.UploadFileBo;
import com.mresources.common.dto.vo.FileListVO;
import com.mresources.common.dto.vo.UploadFileVO;
import com.mresources.resources.service.FileService;
import com.mresources.web.service.ResourcesService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/file")
public class FileController {
    @Resource
    private FileService fileService;
    @Resource
    private ResourcesService resourcesService;

    @GetMapping("/list")
    @PreAuthorize("@auth.matchAny('normal')")
    public Result<FileListVO> fileList() {
        FileListVO files = fileService.listFiles("");
        return Result.success(200, "success").withData(files);
    }

    @GetMapping("/upload")
    public Result<UploadFileVO> preUpload(UploadFileBo uploadFileBo) {
        UploadFileVO fileVO = fileService.createFile(uploadFileBo);
        return Result.success().withData(fileVO);
    }

    @PostMapping("/upload")
    public Result<UploadFileVO> upload(HttpServletRequest request, UploadFileBo uploadFileDto) {
        UploadFileVO fileVO = resourcesService.handleUploadFile(request, uploadFileDto);
        return Result.success().withData(fileVO);
    }

}
